/*
    Description:    This controls the campus tabs (locations).
    Author:         Carlos Krefft (krefftc@gmail.com)
*/
FIUMAPS.Tabs = { OtherCampusText: "Other Campuses",
    Campuses: { MainCampuses: [{ ID: 1, ListItem: null }, { ID: 2, ListItem: null}],
        OtherCampuses: [{ ID: 3, ListItem: null }, { ID: 4, ListItem: null }, { ID: 5, ListItem: null }, { ID: 6, ListItem: null}]
    },

    initiateTabs: function() {
        //create main campus tabs
        for (var z = 0; z < this.Campuses.MainCampuses.length; z++) {
            for (var x = 0; x < FIUMAPS.CampusInfo.arrData.length; x++) {
                // if data matches, you've found the correct campus.
                if (this.Campuses.MainCampuses[z].ID == FIUMAPS.CampusInfo.arrData[x].ID) {
                    //save listitem in memory for later reference:
                    this.Campuses.MainCampuses[z].ListItem = this.createMainCampusListItem(FIUMAPS.CampusInfo.arrData[x]);
                    if ($('Nav_MainCampuses')) $('Nav_MainCampuses').appendChild($(this.Campuses.MainCampuses[z].ListItem));
                }
            }
        }
        //create other campuses pulldown.

        for (var z = 0; z < this.Campuses.OtherCampuses.length; z++) {
            for (var x = 0; x < FIUMAPS.CampusInfo.arrData.length; x++) {
                // if data matches, you've found the correct campus.
                if (this.Campuses.OtherCampuses[z].ID == FIUMAPS.CampusInfo.arrData[x].ID) {
                    //save listitem in memory for later reference:
                    this.Campuses.OtherCampuses[z].ListItem = this.createOtherCampusListItem(FIUMAPS.CampusInfo.arrData[x]);

                    if ($('Nav_MainCampuses')) $('Nav_OtherCampuses').appendChild(this.Campuses.OtherCampuses[z].ListItem);
                }
            }
        }

        //set default campus for nimrod IE.
        if (!URLDecode(getQuerystring('cid'))) {
            this.Campuses.MainCampuses[0].ListItem.addClass('active');
        }
        

    },
    createOtherCampusListItem: function(campus_data) {
        var listItem = document.createElement('li');
        var href = document.createElement('a');

        href.appendChild(document.createTextNode(campus_data.Name));

        href.onclick = Function("FIUMAPS.Tabs.Click('" + campus_data.ID + "')");

        listItem.appendChild(href);

        return listItem;
    },
    createMainCampusListItem: function(campus_data) {
        var listItem = document.createElement('li');
        var href = document.createElement('a');
        var em = document.createElement('em');

        em.appendChild(document.createTextNode(campus_data.Name));

        href.onclick = Function("FIUMAPS.Tabs.Click('" + campus_data.ID + "')");

        href.appendChild(em);
        listItem.appendChild(href);

        return listItem;
    },
    isMainCampus: function(campus_id) {
        //loop through all main campuses looking for ID.
        for (var z = 0; z < this.Campuses.MainCampuses.length; z++) {
            if (this.Campuses.MainCampuses[z].ID == campus_id) {
                return true;
            }
        }
        //if it's gotten this far, it wasn't found in main campus.
        return false;
    },
    Click: function(campus_id) {
        FIUMAPS.CampusInfo.goToCampusByID(campus_id);
        FIUMAPS.LocationsByName.Reset();
        //FIUMAPS.LocationsByCategory.Reset();
        this.applyCSS(campus_id);

    },
    applyCSS: function(campus_id) {
        if (this.isMainCampus(campus_id)) {
            for (var z = 0; z < this.Campuses.MainCampuses.length; z++) {
                if (FIUMAPS.CampusInfo.arrData[z].ID == campus_id) {
                    //remove active class from "other campuses"
                    if ($('selectedLocation')) $('selectedLocation').removeClass('active');
                    if ($('selectedLocation')) $('selectedLocation').innerHTML = this.OtherCampusText;
                    if ($('title_campus_heading')) $('title_campus_heading').innerHTML = FIUMAPS.CampusInfo.arrData[z].Name;
                    if (this.Campuses.MainCampuses[z].ListItem) this.Campuses.MainCampuses[z].ListItem.addClass('active');

                } else {
                    if (this.Campuses.MainCampuses[z].ListItem) this.Campuses.MainCampuses[z].ListItem.removeClass('active');
                }
            }
        } else {
            // campus is in "other campus" tab
            //loop through all campus info looking for requested campus id.
            for (var z = 0; z < FIUMAPS.CampusInfo.arrData.length; z++) {
                if (FIUMAPS.CampusInfo.arrData[z].ID == campus_id) {
                    if ($('selectedLocation')) $('selectedLocation').innerHTML = FIUMAPS.CampusInfo.arrData[z].Name;
                    if ($('selectedLocation')) $('selectedLocation').addClass('active');
                    if ($('title_campus_heading')) $('title_campus_heading').innerHTML = FIUMAPS.CampusInfo.arrData[z].Name;

                    //remove active classes from "Main Campuses"
                    for (var y = 0; y < this.Campuses.MainCampuses.length; y++) {
                        if (this.Campuses.MainCampuses[y].ListItem) this.Campuses.MainCampuses[y].ListItem.removeClass('active');
                    }
                    return;
                }
            }
        }
    }
};

